package com.ziyear.ssm.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.ziyear.ssm.bean.User;

public class UserInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		// 判断是否是登录请求,如果是放行
		if (request.getRequestURL().toString().contains("login.action")) {
			return true;
		}
		//拦截用户请求,判断是否登录
		User user = (User) request.getSession().getAttribute("user");
		if(user != null) {
			return true;
		}
		//否则转发到登录页面
		response.sendRedirect(request.getContextPath());
		return false;
	}
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	

}
